package com.xyoto.community.utils;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Map;

/**
 * 寻氧O2社区短信服务
 */
@Service
public class XYOtoMsmUtils implements XYOtoMsm{

    private static String endpoint = "cn-hangzhou";

    private static String accessKeyId = "LTAI5tDS2wNnfdhbNTjKznyv";

    private static String accessSecret = "AKvCXhQLoonczR69NJcbD5MBVWAxet";

    /**
     * 发送短信验证码
     * @param param 验证码
     * @param phone 手机号
     * @return
     */
    public boolean send(Map<String, Object> param, String phone){

        DefaultProfile profile = DefaultProfile.getProfile(endpoint,accessKeyId,accessSecret);
        IAcsClient client = new DefaultAcsClient(profile);
        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);

        //域名，勿改
        request.setSysDomain("dysmsapi.aliyuncs.com");
        //API版本号，请勿修改
        request.setSysVersion("2017-05-25");
        //API名称
        request.setSysAction("SendSms");
        //接收号码，格式为：号码，必填
        request.putQueryParameter("PhoneNumbers", phone);
        //申请阿里云 签名名称 寻氧
        request.putQueryParameter("SignName","寻氧");
        //申请阿里云 模板code SMS_ 219890080
        request.putQueryParameter("TemplateCode","SMS_219890080");
        //短信内容，必填
        request.putQueryParameter("TemplateParam", JSONObject.toJSONString(param));
        try {
            CommonResponse response = client.getCommonResponse(request);
            return response.getHttpResponse().isSuccess();
        } catch (ClientException e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 批量发送短信
     * @param content 短信内容
     * @param phone 手机号
     * @return
     */
    public boolean sendBatch(String content, String[] phone){

        DefaultProfile profile = DefaultProfile.getProfile(endpoint,accessKeyId,accessSecret);
        IAcsClient client = new DefaultAcsClient(profile);
        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);

        //域名，勿改
        request.setSysDomain("dysmsapi.aliyuncs.com");
        //API版本号，请勿修改
        request.setSysVersion("2017-05-25");
        //API名称
        request.setSysAction("SendBatchSms");
        //接收号码，格式为：国际码+号码，必填
        request.putQueryParameter("PhoneNumberJson", Arrays.toString(phone));
        //申请阿里云 签名名称
        request.putQueryParameter("SignNameJson","寻氧");
        //申请阿里云 模板code
        request.putQueryParameter("TemplateCode","SMS_219890080");
        //短信内容，必填
        request.putQueryParameter("TemplateParamJson", JSONObject.toJSONString(content));
        try {
            CommonResponse response = client.getCommonResponse(request);
            return response.getHttpResponse().isSuccess();
        } catch (ClientException e) {
            e.printStackTrace();
            return false;
        }

    }

}
